home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
machack
/
Hacks96
/
BootingGallery.sit
/
Booting Gallery
/
Booting Gallery (source)
/
Sources
/
Duck Game Sources
/
DuckGame.cpp
next >
Wrap
Text File
|
1996-06-22
|
4KB
|
218 lines
/***
* Created by Bill Hubauer on Fri, Jun 21, 1996 @ 2:25 AM.
*
***/
#ifndef __DuckGame_H__
#include "DuckGame.h"
#endif
#include "GunSprite.h"
#include "GWorldUtils.h"
CSprite* CDuckGame::MakeExtensionSprite(GWorldPtr image,RgnHandle mask) //Override
{
return new CDuckSprite(GetWorld(), this, image, mask);
}
OSErr CDuckGame::Initialize()//Override
{
OSErr err = noErr;
err = inherited::Initialize();
if(err == noErr){
new CGunSprite(GetWorld(), this);
PlaySound(1002,true);
}
return err;
}
CDuckGame::CDuckGame()
: CSpriteGame(128)
{
}
CDuckGame::~CDuckGame()//Override
{
}
static short Abs(short v)
{
if(v < 0){
return -v;
}else{
return v;
}
}
static short RandomInRange(short lo,short hi)
{
if(hi == lo){
return lo;
}
if(hi < lo){
short t = lo;
lo = hi;
hi = t;
}
return (Abs(Random()) % (hi - lo + 1)) + lo;
}
static short lastDuckOffset;
static short CalcDuckTop()
{
short result = CDuckSprite::kDuckStartTop;
short curDuckOffset;
for (;;) {
curDuckOffset = RandomInRange(-100,150);
if (curDuckOffset < lastDuckOffset - 40) break;
if (curDuckOffset > lastDuckOffset + 40) break;
}
lastDuckOffset = curDuckOffset;
result += curDuckOffset;
return result;
}
CDuckSprite::CDuckSprite(CSpriteWorld* world,CSpriteGame* game,GWorldPtr image,
RgnHandle mask)
: CGameSprite(world, game, 0, NULL, CalcDuckTop(), kDuckStartLeft, NULL),
fCurDuck(0)
{
if(BuildDuckImage(image,mask) == noErr){
SetImage(fDucks[0].image, fDucks[0].mask);
}
}
CDuckSprite::~CDuckSprite()
{
}
void CDuckSprite::UpdatePosition() //Override
{
SetNextDuck();
MoveBy(kMoveHInterval,0);
if(OutOfBoundsQ()){
MoveToH(0);
}
}
Boolean CDuckSprite::WasHitBy(CSprite* thisOne)//Override
{
#ifdef OM
#else
CBulletSprite* bullet = dynamic_cast<CBulletSprite*>(thisOne);
#endif
if(bullet != NULL){
PlaySound(kQuackID);
delete this;
delete thisOne;
return false;
}else{
return true;
}
}
OSErr CDuckSprite::BuildDuckImage(GWorldPtr iconImage,RgnHandle iconMask)
{
OSErr err = CopyBaseImage(kDuck1,fDucks[0].image,fDucks[0].mask);
if(err == noErr){
err = CopyBaseImage(kDuck2,fDucks[1].image,fDucks[1].mask);
if(err == noErr){
err = Superimpose(iconImage,iconMask,fDucks[0].image,fDucks[0].mask);
if(err == noErr){
err = Superimpose(iconImage,iconMask,fDucks[1].image,fDucks[1].mask);
}
}
}
return err;
}
void CDuckSprite::SetNextDuck()
{
fCurDuck++;
if(fCurDuck == 2){
fCurDuck = 0;
}
SetImage(fDucks[fCurDuck].image,fDucks[fCurDuck].mask);
}
OSErr CDuckSprite::CopyBaseImage(short id,GWorldPtr& outGWorld,RgnHandle& outMask)
{
OSErr err = noErr;
GWorldPtr base;
RgnHandle mask;
StSaveGWorld save;
if(GetGame()->GetImage(id,base,mask)){
Rect r = base->portRect;
err = NewLockedGWorld(&outGWorld, 0, &r, NULL, NULL, 0);
if(err == noErr){
SetGWorld(outGWorld,NULL);
CopyBits((BitMap*)*base->portPixMap,(BitMap*)*outGWorld->portPixMap,&r,&r,srcCopy,NULL);
outMask = NewRgn();
CopyRgn(mask,outMask);
HLock((Handle)outMask);
}
}else{
err = paramErr;
}
return err;
}
OSErr CDuckSprite::Superimpose(GWorldPtr iconImage,RgnHandle iconMask,GWorldPtr duck,RgnHandle duckMask)
{
OSErr err = noErr;
StSaveGWorld st;
Rect r = iconImage->portRect;
SetGWorld(duck,NULL);
CopyBits((BitMap*)*iconImage->portPixMap,(BitMap*)*duck->portPixMap,&r,&r,srcCopy,iconMask);
HUnlock((Handle)duckMask);
UnionRgn(iconMask,duckMask,duckMask);
HLock((Handle)duckMask);
return err;
}
void CDuckGame::DrawBackground(const Rect& inBounds)
{
PicHandle h = GetPicture(1000);
if(h != NULL){
Rect r = h[0]->picFrame;
OffsetRect(&r,-r.left,-r.top);
DrawPicture(h,&r);
}
}